package app.com.itcast.biyesheji.Activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import app.com.itcast.biyesheji.Bean.UsercgBean;
import app.com.itcast.biyesheji.R;

public class WoderightActivity extends AppCompatActivity {
    private ImageView shouye;
    private TextView name;
    private ImageView car;
    private int userid;
    private TextView tuichu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_woderight);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        shouye = findViewById(R.id.bottom_bar_image_course);
        car = findViewById(R.id.bottom_bar_image_exercises);
        name = findViewById(R.id.tv_user_name);
        tuichu = findViewById(R.id.tv_tuichu);
    }

    private void initData() {
        SharedPreferences userinfo = getSharedPreferences("data",MODE_PRIVATE);
        userid = userinfo.getInt("userid",0);
        getData();

    }

    private void getData() {

        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        String url = getResources().getString(R.string.api_ip) + "/denlu/momo/findone";
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("userid", userid);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e("TAG", "onResponse: " + jsonObject);
                /*处理结果数据*/
                // 1）讲json对象转换为java对象（需要使用GsonFormat插件根据返回数据，创建结果处理类MyBean）
                Gson gson = new Gson();
               // MyBean myBean = gson.fromJson(jsonObject.toString(), MyBean.class);
                UsercgBean myBean = gson.fromJson(jsonObject.toString(),UsercgBean.class);
                if(myBean.isSuccess()){
                    // 获取数据，
                    UsercgBean.DataBean dataBean = myBean.getData();
                    // 在控件上显示文字
                    name.setText(dataBean.getPhone());

                }else{
                    Toast.makeText(getApplicationContext(),  "失败", Toast.LENGTH_SHORT).show();
                }
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getApplicationContext(), "网络错误", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }
                 private void showExitDialog02(){
                        new AlertDialog.Builder(this)
                         .setTitle("退出框")
                         .setMessage("你是否要退出？")
                         .setPositiveButton("是", new DialogInterface.OnClickListener() {
                             @Override
                             public void onClick(DialogInterface dialog, int which) {
                             Intent intent = new Intent(WoderightActivity.this,WodeActivity.class);
                             startActivity(intent);
                             }
                         })
                         .setNegativeButton("否", null).show();
                     }
    private void initEvent() {
        shouye.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences userinfo = getSharedPreferences("data",MODE_PRIVATE);
                SharedPreferences.Editor usid =userinfo.edit();
                usid.putInt("userid",userid);
                usid.commit();
                Intent intent = new Intent(WoderightActivity.this,ShouyeActivity.class);
                startActivity(intent);
            }
        });
        car.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(WoderightActivity.this,CarActivity.class);
                startActivity(intent);
            }
        });
        tuichu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showExitDialog02();
            }
        });

    }
}